package keer.httpinterface.http;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * http请求体封装类
 * @see HttpContentType
 *
 * @author Keer
 * @since 1.0.0
 */
@SuppressWarnings({"NullableProblems"})
public class HttpBody extends InputStream {
    /**
     * 请求体的Content-Type
     * @see HttpContentType
     */
    private final HttpContentType contentType;
    /**
     * 请求体内容流
     */
    private final InputStream stream;

    /**
     * 私有构造方法，通过静态方法构造实例
     * @param contentType 请求体Content-Type
     * @param stream 请求体内容流
     */
    private HttpBody(HttpContentType contentType, InputStream stream) {
        this.contentType = contentType;
        this.stream = stream;
    }

    /**
     * 通过流构造请求体
     * @param contentType 请求体Content-Type
     * @param stream 请求体内容流
     * @return 请求体实例
     */
    public static HttpBody streamBody(HttpContentType contentType,InputStream stream) {
        return new HttpBody(contentType, stream);
    }

    /**
     * 通过字节数组构造请求体
     * @param contentType 请求体Content-Type
     * @param bytes 字节数组
     * @return 请求体实例
     */
    public static HttpBody bytesBody(HttpContentType contentType, byte[] bytes) {
        return new HttpBody(contentType, new ByteArrayInputStream(bytes));
    }

    @Override
    public int read() throws IOException {
        return stream.read();
    }

    /**
     * 获取请求体的Content-Type
     * @see HttpContentType
     * @return HttpContentType
     */
    public HttpContentType getContentType() {
        return contentType;
    }
}
